package com.lanmaoly.web.server.controller;

import com.lanmaoly.web.server.dao.TbAddressMapper;
import com.lanmaoly.web.server.entity.PageResult;
import com.lanmaoly.web.server.entity.Result;
import com.lanmaoly.web.server.entity.UserDetailsEntity;
import com.lanmaoly.web.server.pojo.TbAddress;
import com.lanmaoly.web.server.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * controller
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;
    @Autowired
    private TbAddressMapper tbAddressMapper;

    @RequestMapping(value = "list")
    public String list(Model model) {
        UserDetailsEntity entity = (UserDetailsEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        model.addAttribute("username", entity.getUsername());
        model.addAttribute("isLogin", true);
        List<TbAddress> list = addressService.findListByUserId(entity.getUsername());
        model.addAttribute("list", list);
        model.addAttribute("headPic", entity.getHeadPic());
        return "portal/home-setting-address";
    }

    @ResponseBody
    @RequestMapping("/deleteById")
    public Result deleteById(Long id) {
        try {
            tbAddressMapper.deleteByPrimaryKey(id);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

    @ResponseBody
    @RequestMapping("/update")
    public Result update(TbAddress address) {
        try {
            addressService.update(address);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }

    @ResponseBody
    @RequestMapping("/setDefault")
    public Result setDefault(Long id) {
        addressService.setDefault(id);
        return Result.success();
    }

    @ResponseBody
    @RequestMapping("/add")
    public Result add(TbAddress address) {
        try {
            address.setUserId(SecurityContextHolder.getContext().getAuthentication().getName());
            address.setCreateDate(new Date());
            addressService.add(address);
            return Result.success("增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "增加失败");
        }
    }

    /**
     * 返回全部列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/findAll")
    public List<TbAddress> findAll() {
        return addressService.findAll();
    }


    /**
     * 返回全部列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/findPage")
    public PageResult findPage(int page, int rows) {
        return addressService.findPage(page, rows);
    }

    /**
     * 获取实体
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/findOne")
    public TbAddress findOne(Long id) {
        return addressService.findOne(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Result delete(Long[] ids) {
        try {
            addressService.delete(ids);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

    /**
     * 查询+分页
     *
     * @param page
     * @param rows
     * @return
     */
    @ResponseBody
    @RequestMapping("/search")
    public PageResult search(@RequestBody TbAddress address, int page, int rows) {
        return addressService.findPage(address, page, rows);
    }

    @ResponseBody
    @RequestMapping("/findListByLoginUser")
    public List<TbAddress> findListByLoginUser() {
        //获取登陆用户
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        return addressService.findListByUserId(username);
    }

}
